#ifdef KINECT

#include "inc\GUI\kinect\kinectcalibrationdialog.h"
#include "ui_kinectcalibrationdialog.h"

#include <QPushButton>

#include "Kinect/kinectimage.h"

#ifdef WIIMOTE
#include "GUI/wiimote/wiimotestatusdialog.h"
#endif

KinectCalibrationDialog::KinectCalibrationDialog(kinect::Kinect* kinect, QWidget *parent) :
    KinectDialog(kinect, parent),
    ui(new Ui::KinectCalibrationDialog),
    closeTimer(-1)
{
    ui->setupUi(this);
    ui->progressBar->setMinimum(0);
    ui->progressBar->setMaximum(3);

    ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
    connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(reject()));

    //_troyPreview = new TroyFrame(ui->troyPreview);
    //_troyPreview->resize(ui->troyPreview->size());
    ui->troyPreview->loadKeyFrame(troyStateToKeyFrame(kinect::TroyState::calibrationPose(this)));
    ui->troyPreview->mirror(true);
    switch (getKinectSkeleton()->trackingState()) {
    case kinect::KinectSkeleton::Waiting:
        searchForUser();
    break;
    case kinect::KinectSkeleton::Watching:
        searchForPose();
    break;
    case kinect::KinectSkeleton::Calibrating:
        holdPose();
    break;
    case kinect::KinectSkeleton::Tracking:
        calibrated();
    break;
    }

    connect(getKinectSkeleton(), SIGNAL(candidateFound()), this, SLOT(searchForPose()));
    connect(getKinectSkeleton(), SIGNAL(candidateLost()), this, SLOT(searchForUser()));
    connect(getKinectSkeleton(), SIGNAL(candidateFocusing()), this, SLOT(holdPose()));
    connect(getKinectSkeleton(), SIGNAL(candidateFocused(bool)), this, SLOT(calibrated(bool)));

    kinect::KinectImage* image = getKinect()->addComponent<kinect::KinectImage>();
    connect(image, SIGNAL(depthMapChanged(QImage*)), this, SLOT(updateImage(QImage*)));

    raise();

    maximizeSize();
}

KinectCalibrationDialog::~KinectCalibrationDialog()
{
    delete ui;
}

#ifdef WIIMOTE
void KinectCalibrationDialog::associateWiimote(WiimoteStatusDialog *dialog) {
    if (hasWiimoteDialog()) {
        getWiimoteDialog()->setCommand(qWiimote::Minus, "Cancel", this, SLOT(reject()));
        getWiimoteDialog()->setCommand(qWiimote::B, "Cancel", this, SLOT(reject()));
        getWiimoteDialog()->setCommand(qWiimote::Home, "Reset", getKinectSkeleton(), SLOT(reset()));
        // Link success with a positive response
        getWiimoteDialog()->linkPositiveFeedback(this, SIGNAL(accepted()));
    }
}
#endif

void KinectCalibrationDialog::searchForUser() {
    ui->progressBar->setValue(0);
    ui->text->setText("The Kinect has not found a user in its range. Stand about 5 feet in front of the Kinect, and if it still can't find you, move back and forth a little bit so it can notice your movements.");
}

void KinectCalibrationDialog::searchForPose() {
    ui->progressBar->setValue(1);
    ui->text->setText("A user has been found. In order to calibrate the Kinect, please mimic the pose Troy is currently making, and hold it until it is detected and calibrated.");
}

void KinectCalibrationDialog::holdPose() {
    ui->progressBar->setValue(2);
    ui->text->setText("Troy's pose has been detected, and is now being calibrated. Please hold that pose until it has been calibrated completely.");
}

void KinectCalibrationDialog::calibrated(bool success) {
    if (success) {
        ui->progressBar->setValue(3);
        ui->text->setText("The Kinect has been successfully calibrated! You may now close this dialog and continue to the rest of the application.\n\nIf you do not close it by yourself, it will automatically close now.");
        ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(false);
    #ifdef WIIMOTE
        if (hasWiimoteDialog()) {
            getWiimoteDialog()->setCommand(qWiimote::Plus, "OK", this, SLOT(accept()));
        }
    #endif
        closeTimer = startTimer(0); //5
    } else {
        searchForPose();
    }
}

void KinectCalibrationDialog::updateImage(QImage *image) {
    ui->kinectCamera->setPixmap(QPixmap::fromImage(*image));
}

void KinectCalibrationDialog::kinectKeyFrame(MotionKeyFrame keyFrame) {
    ui->troyPreview->loadKeyFrame(keyFrame);
}

void KinectCalibrationDialog::timerEvent(QTimerEvent *event) {
    KinectDialog::timerEvent(event);
    if (event->timerId() == closeTimer) {
        accept();
        closeTimer = -1;
    }
}

void KinectCalibrationDialog::hideEvent(QHideEvent *event) {
    // Removes KinectDialog calibration checks
}

void KinectCalibrationDialog::showEvent(QShowEvent *event) {
    // Removes KinectDialog calibration checks
}

#endif
